package com.controller;

import com.exception.DataAccessException;
import com.github.pagehelper.PageInfo;
import com.service.CommentService;
import com.util.AjaxResult;
import com.vo.CommentVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    CommentService commentService;


    /**
     * 查询待审核评论
     */
    @RequestMapping("/findComs.do")
    @ResponseBody
    public AjaxResult findAwaitingComment(@RequestBody CommentVo commentVo) {

        AjaxResult ajaxResult = new AjaxResult(true, "查询未审批评论成功", null);
        try {
            PageInfo<CommentVo> commentByStatus = commentService.findCommentByStatusAndCondition(commentVo);
            ajaxResult.setObj(commentByStatus);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg(e.getMessage());
        }
        return ajaxResult;

    }

    /**
     * 通过审核禁用评论,通过评论id修改评论的status参数：通过：0;禁用：1 ,/toggle.do
     */
    @RequestMapping("/toggle.do")
    @ResponseBody
    public int passAndDisableComment(@RequestParam("id") Integer commentId, Integer status) {
        Integer i = 0;

        try {
            i = commentService.modifyStatusByCommentId(commentId, status);
        } catch (Exception e) {
            e.printStackTrace();
            throw new DataAccessException("修改评论状态失败");
        }
        return i;
    }

    /**
     * 根据条件分页查询已审核评论
     *              "login_name" : name,
     * 				"begin_date" : beginDate,
     * 				"end_date" : endDate,
     * 				"context" : context,
     * 				"pageNo" : pageNo,
     * 				"status":status,
     * 				"chapter_id":chapter_id
     * 			/comment/findByChapterId.do
     */
    @RequestMapping("findByChapterId.do")
    @ResponseBody
    public AjaxResult findCheckedCommentByCondition(@RequestBody CommentVo commentVo){
        AjaxResult ajaxResult = new AjaxResult(true, "查询已审批评论成功", null);
        try {
            PageInfo<CommentVo> commentByStatus = commentService.findCommentByStatusAndCondition(commentVo);
            ajaxResult.setObj(commentByStatus);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            ajaxResult.setMsg(e.getMessage());
        }
        return ajaxResult;

    }


}
